package cn.rui.controller;

import cn.rui.aop.Log;
import cn.rui.pojo.User;
import cn.rui.service.UserService;
import cn.rui.vo.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 徽州大都督
 * @date 2020/8/15
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("register")
    @Log("添加会员")
    public String register(@RequestBody User user) {

        //System.out.println (user);
        int row = userService.register (user);
        if (row > 0) {
            return "success";
        } else {
            return "erroe";
        }

    }

    /**
     * 分页查询用户
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping("findByPage/{page}/{size}")
    public List<User> findByPage(@PathVariable("page") int page, @PathVariable("size") int size, @RequestBody UserQuery userQuery) {

        System.out.println (page);
        System.out.println (size);
        System.out.println (userQuery);

        return userService.findByPage (page, size,userQuery);


    }

    /**
     * 修改用户状态
     *
     * @param uid
     */
    @Log("启用/封禁用户")
    @PutMapping("updateStutas/{uid}")
    public void updateStutas(@PathVariable("uid") Integer uid) {
        userService.updateStutas (uid);
    }

    /**
     * 删除用户
     *
     * @param uid
     */
    @Log("删除会员账号")
    @DeleteMapping("deleteUser/{uid}")
    public void deleteUser(@PathVariable("uid") Integer uid) {
        userService.deleteUser (uid);
    }

    /**
     * 根据id查询
     *
     * @param uid
     * @return
     */
    @GetMapping("findById/{uid}")
    public User findById(@PathVariable("uid") Integer uid) {
        return userService.findById (uid);
    }



    /**
     * 批量删除
     */
    @Log("批量删除会员")
    @DeleteMapping("delAll/{users}")
    public void delAll(@PathVariable("users")List<Integer> users){
        userService.delAll(users);
    }

}
